﻿#region COPYRIGHT
//--------------------------------------------------------------------------------
// <copyright file="RandomScaleAll.cs" company="starLiGHT Entertainment Studios">
//    Copyright (c) 2008, 2009, 2010, 2011
//       Roland Rosenkranz (Glatzemann@email.de)
// </copyright>
// <license>
//   This file is part of starLiGHT.Particles.
//
//   starLiGHT.Particles is free software: you can redistribute it and/or modify
//   it under the terms of the GNU Lesser General Public License as published by
//   the Free Software Foundation, either version 3 of the License, or
//   (at your option) any later version.
//
//   starLiGHT.Particles is distributed in the hope that it will be useful,
//   but WITHOUT ANY WARRANTY; without even the implied warranty of
//   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
//   GNU Lesser General Public License for more details.
//
//   You should have received a copy of the GNU Lesser General Public License
//   along with starLiGHT.Particles. If not, see http://www.gnu.org/licenses/.
//    
//   ADDITIONAL (commercial) LICENSES for starLiGHT.Particles are available on request.
// </license>
// <original_license>
//  * FLINT PARTICLE SYSTEM
//  * .....................
//  * 
//  * Author: Richard Lord
//  * Copyright (c) Richard Lord 2008-2009
//  * http://flintparticles.org
//  * 
//  * 
//  * Licence Agreement
//  * 
//  * Permission is hereby granted, free of charge, to any person obtaining a copy
//  * of this software and associated documentation files (the "Software"), to deal
//  * in the Software without restriction, including without limitation the rights
//  * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
//  * copies of the Software, and to permit persons to whom the Software is
//  * furnished to do so, subject to the following conditions:
//  * 
//  * The above copyright notice and this permission notice shall be included in
//  * all copies or substantial portions of the Software.
//  * 
//  * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
//  * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
//  * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
//  * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
//  * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
//  * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
//  * THE SOFTWARE.
// </original_license>
// <version>
// **************[ starLiGHT.Engine SVN ]**********************
// * $Rev::                       $:  Revision of last commit *
// * $Author::                    $:  Author of last commit   *
// * $Date::                      $:  Date of last commit     *
// ************************************************************
// </version>
//--------------------------------------------------------------------------------
#endregion

namespace starLiGHT.ParticleSystem.Initializer.TwoD
{
    #region Using Statements
    using System;
    using System.Collections.Generic;
    using System.Text;
    using starLiGHT.Collections;
    using starLiGHT.ParticleSystem.Common;
    using starLiGHT.ParticleSystem.Initializer;
    #endregion

    /// <summary>
    /// The RandomScaleAll Initializer holds a list of scales which are optionally weighted by a factor 
    /// and these are used to set the scale of the particle and its mass and collision radius accordingly.
    /// During initialization of a particle the RandomScaleAll initializer chooses randomly one of it's
    /// scales taking the weighting into account. If you have a scale of 0.5f with a weight 0.9f 
    /// and a scale of 2.0f with a weight 0.1f in the list, there's a 90% chance that the 0.5f scale
    /// and a 10% chance that the 2.0f scale is choosen.
    /// </summary>
    /// <remarks>
    /// <para>If you want to adjust the particles scale only use the Scale Initializer.</para>
    /// <para>This initializer has a priority of -10 to ensure it occurs after mass and radius assignment 
    /// classes like CollisionRadius and Mass.</para>
    /// </remarks>
    public class RandomScaleAll : InitializerBase
    {
        #region Member variables
        private WeightedList<float> _scales;

        #endregion

        #region Constructors
        /// <summary>
        /// The constructor creates a RandomScale initializer to be used by an emitter.
        /// </summary>
        public RandomScaleAll()
        {
            Priority = -10;
            _scales = new WeightedList<float>();
        }

        /// <summary>
        /// The constructor creates a RandomScale initializer to be used by an emitter.
        /// </summary>
        public RandomScaleAll(List<WeightValuePair<float>> weightValuePairs)
            : this()
        {
            if (weightValuePairs == null)
            {
                return;
            }

            foreach (WeightValuePair<float> c in weightValuePairs)
            {
                _scales.Add(c);
            }
        }

        /// <summary>
        /// The constructor creates a RandomScale initializer to be used by an emitter.
        /// </summary>
        public RandomScaleAll(List<float> weightValuePairs)
            : this()
        {
            if (weightValuePairs == null)
            {
                return;
            }

            foreach (float f in weightValuePairs)
            {
                _scales.Add(1.0f, f);
            }
        }

        #endregion

        public WeightedList<float> Scales
        {
            get { return _scales; }
        }

        /// <summary>
        /// Adds a new scale to the weighted list of scales.
        /// </summary>
        /// <param name="scale">The scale to be added. It has a default weight of 1.0f.</param>
        public void addScale(float scale)
        {
            _scales.Add(1.0f, scale);
        }

        /// <summary>
        /// Adds a new scale to the weighted list of scales.
        /// </summary>
        /// <param name="scale">The scale to be added.</param>
        /// <param name="weight">The weight that this scale should have.</param>
        public void addScale(float scale, float weight)
        {
            _scales.Add(weight, scale);
        }

        /// <summary>
        /// The initialize method is used by the emitter to apply the initialization
        /// to every particle. It is the key feature of the initializers and is
        /// used to initialize the state of every particle. This method 
        /// is called within the emitter's createParticle met6hod for every particle 
        /// and need not be called by the user.
        /// </summary>
        /// <param name="emitter">The Emitter that created the particle.</param>
        /// <param name="particle">The particle to be initialized.</param>
        /// <remarks>This is an internal method that is called by the emitter.</remarks>
        public override void initialize(Emitter emitter, ref Particle particle)
        {
            float scale = _scales.RandomValue;
            particle.scale = scale;
            particle.mass *= scale * scale;
            particle.collisionRadius *= scale;
        }
    }
}
